﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Web.Mvc;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Slick.Editors;
using SlickGridNet;

namespace SlickGridNet_UnitTest
{
	[TestClass]
	public class SlickGridUnitTest
	{
		[TestMethod]
		public void RenderColumnsTest()
		{
			var expectedRender =
@"[
	{
		name: ""Some"",
		id: ""some"",
		field: ""SomeField"",
		editor: Slick.Editors.Text
	},
	{
		name: ""Name"",
		id: ""name"",
		field: ""NameField"",
		resizable: true
	}
]";

			var slickGrid = new SlickGrid();
			slickGrid
				.AddColumn(new SlickColumn("Some", "some", "SomeField") { Editor = new TextEditor() })
				.AddColumn(new SlickColumn("Name", "name", "NameField") { Resizable = true });

			var result = slickGrid.RenderColumns(true);

			Assert.IsNotNull(result, "Null result of SlickGrid.RenderColumns");
			Assert.AreEqual(expectedRender, result, "RenderColumns result is not as expected.");

			result = slickGrid.RenderColumns();
			//Test for unformatted output.
			Assert.AreEqual(
				expectedRender.Replace("\r\n", "").Replace("\t", "").Replace(": ", ":"), result,
				"Unformatted output incorrect");
		}

		[TestMethod]
		public void FullRenderTest()
		{
			const string slickName = "MySlick";
			var expectedRender = String.Format(
@"<script type=""text/javascript"" language=""javascript"">
var {0};
var {0}Columns = [{{name:""Some"",id:""some"",field:""SomeField"",editor:Slick.Editors.DropdownList}},{{name:""Name"",id:""name"",field:""NameField"",resizable:true}}];
var {0}Options = {{editable:true}};
var {0}Data = null;
$(function () {{
	{0} = new Slick.Grid(""#MySlick"", {0}Data, {0}Columns, {0}Options);
}});
</script>", slickName);

			var slickGrid = new SlickGrid(slickName, new SlickOptions { Editable = true })
				.AddColumn(new SlickColumn("Some", "some", "SomeField") { Editor = new Slick.Editors.DropdownListEditor() })
				.AddColumn(new SlickColumn("Name", "name", "NameField") { Resizable = true });

			var result = slickGrid.Render();
			Assert.IsNotNull(result, "SlickGrid.Render result shouldn't be null.");

			Assert.AreEqual(expectedRender, result, "SlickGrid.Render is not as expected.");
		}

		[TestMethod]
		public void RenderSlickOptionsTest()
		{
			string expectedResult = 
@"{
	editable: true,
	rowHeight: 101,
	enableAddRow: true
}";
			var slickGrid = new SlickGrid();
			slickGrid.Options = new SlickOptions() { Editable = true, RowHeight = 101 };

			Assert.IsTrue(slickGrid.Options["editable"] is bool);
			Assert.AreEqual((bool)slickGrid.Options["editable"], true);

			slickGrid.Options.EnableAddRow = true;

			//Test for formatted render.
			string result = slickGrid.RenderOptions(true);
			Assert.AreEqual(expectedResult, result);

			result = slickGrid.RenderOptions();
			Assert.AreEqual(expectedResult.Replace("\t", "").Replace("\r\n", "").Replace(": ",":"), result);
		}

		[TestMethod]
		public void ExplicitStringTest()
		{
			string expected = "[{asyncPostRender:function (cellNode, row, dataContext, colDef){;}}]";
			var slick = new SlickGrid().
				AddColumn(new SlickColumn
				{
					AsyncPostRender = new ExplicitString("function (cellNode, row, dataContext, colDef){;}")
				});

			var result = slick.RenderColumns();

			Assert.AreEqual(expected, result);
		}

//		[TestMethod]
//		public void GenerateSlickWithGenericModelTest()
//		{
//			var list = new List<Person>
//			{
//				new Person
//				{
//					Name = "John Doe",
//					BirthDate = new DateTime(1986, 06, 14),
//					Email = "ddd@aaa.ru",
//					Salary = 18000
//				}
//			};
//			SlickGrid.Create(list);
//		}

		[TestMethod]
		public void DefaultNamesTest()
		{
			const string name = "myGrid";
			var slickGrid = new SlickGrid(name);

			Assert.AreEqual(name + "Columns", slickGrid.ColumnsName);
			Assert.AreEqual(name + "Options", slickGrid.OptionsName);
			Assert.AreEqual(name + "Data", slickGrid.DataName);

			try
			{
				slickGrid.ColumnsName = "1qwe";
				Assert.Fail();
			}
			catch (ArgumentException){}
			try
			{
				slickGrid.OptionsName = "$qwe";
				Assert.Fail();
			}
			catch (ArgumentException) { }
			try
			{
				slickGrid.DataName = "%";
				Assert.Fail();
			}
			catch (ArgumentException) { }
		}
	}
}
